package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class DishServiceImpl implements DishService {

    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private DishFlavorMapper dishFlavorMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    /**
     * 新增菜品
     * @param dishDTO
     */
    @Transactional
    public void saveWithFlavor(DishDTO dishDTO) {
        //向菜品表中插入一条数据
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        dishMapper.save(dish);

        //获取菜品自增id
        Long dishId = dish.getId();

        //向口味表中插入多条数据
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if(flavors.size()>0 && flavors != null){
            flavors.forEach(dishFlavor -> {
                dishFlavor.setDishId(dishId);
            });
            dishFlavorMapper.saveBatch(flavors);
        }
    }

    /**
     * 菜品分页查询
     * @param dishPageQueryDTO
     * @return
     */
    public PageResult pageQuery(DishPageQueryDTO dishPageQueryDTO) {
        PageHelper.startPage(dishPageQueryDTO.getPage(),dishPageQueryDTO.getPageSize());
        Page<DishVO> page = dishMapper.pageQuery(dishPageQueryDTO);
        return new PageResult(page.getTotal(),page.getResult());
    }

    /**
     *批量删除菜品
     * @param ids
     */
    @Transactional
    public void delete(List<Long> ids) {

        //判断是否有启售状态，有则抛出异常,并全不删除
        for (Long id : ids) {
            Dish dish = dishMapper.getById(id);
            if(dish.getStatus() == StatusConstant.ENABLE){
                //当前菜品起售中，不能删除
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
        }

        //判断是否关联套餐，有则抛出异常
        List<Long> setmealIds = setmealDishMapper.getSetmealIdsByDishIds(ids);
        if(setmealIds.size()>0 && setmealIds != null){
            throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
        }

        //根据id删除菜品表记录
        dishMapper.deleteBatch(ids);
        //根据菜品id删除口味表记录
        dishFlavorMapper.deleteBatch(ids);
    }

    /**
     * 根据id查询菜品
     * @param id
     * @return
     */
    public DishVO getById(Long id) {
        //查询菜品表
        Dish dish = dishMapper.getById(id);

        //查询口味表
        List<DishFlavor> dishFlavor = dishFlavorMapper.getByDishId(id);

        DishVO dishVO = new DishVO();
        BeanUtils.copyProperties(dish,dishVO);
        dishVO.setFlavors(dishFlavor);

        return dishVO;
    }

    /**
     * 修改菜品
     * @param dishDTO
     */
    @Transactional
    public void update(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        //修改菜品
        dishMapper.update(dish);

        //删除对应菜品口味
        dishFlavorMapper.deleteByDishId(dishDTO.getId());
        //添加对应菜品口味
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if(flavors.size()>0 && flavors != null){
            flavors.forEach(dishFlavor -> {

                dishFlavor.setDishId(dishDTO.getId());
            });
            dishFlavorMapper.saveBatch(flavors);
        }
    }

    /**
     * 菜品起售、停售
     * @param status
     * @param id
     */
    public void changeStatus(Integer status,Long id) {
        Dish dish = Dish.builder()
                .id(id)
                .status(status)
                .build();
        dishMapper.update(dish);
    }

    /**
     * 根据分类id查询菜品
     * @param categoryId
     * @return
     */
    public List<Dish> getDishList(Long categoryId) {
        List<Dish> dishList = dishMapper.getDishList(categoryId);
        return dishList;
    }

    /**
     * 通过分类id查询菜品和口味
     * @param categoryId
     * @return
     */
    public List<DishVO> ListWithFlavor(Long categoryId) {

        List<DishVO> dishVOList = new ArrayList<>();
        List<Dish> dishList = dishMapper.getDishList(categoryId);
        dishList.forEach(dish -> {
            DishVO dishVO = new DishVO();

            //根据dish_id查询对应口味
            List<DishFlavor> flavors = dishFlavorMapper.getByDishId(dish.getId());
            BeanUtils.copyProperties(dish,dishVO);
            dishVO.setFlavors(flavors);
            dishVOList.add(dishVO);
        });
        return dishVOList;
    }
}
